package com.ding.second.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ding.second.po.User;
import com.ding.second.service.UserService;

@Controller
@RequestMapping("login")
public class LoginAction {

	@Autowired
	private UserService userServer;

	@RequestMapping("login")
	public String login(User user, HttpSession session, Model model) {
		Map<String, Object> userAccount = new HashMap<String, Object>();
		userAccount.put("userAccount", user.getUserAccount());
		userAccount.put("userPsw", user.getUserPsw());
		user = userServer.getUserByAccount(userAccount);
		if (user == null || user.getUserId() == 0 || user.getUserState().equals("110")) {
			return "userLogin";
		}
		session.setAttribute("userName", user.getUserName());
		session.setAttribute("userId", user.getUserId());
		if (user.getUserState().equals("100")) {
			model.addAttribute("userAccount",user.getUserAccount());
			model.addAttribute("id", user.getUserId());
			return "personInfo";
		}
		
		return "redirect:/inquiry/all.do";
	}
	
	@RequestMapping("loginOut")
	public String loginOut(HttpSession session){
		session.invalidate();
		return "redirect:/inquiry/all.do";
		
	}
}
